package com.zhuchaooasystem.presenter.home.fragment.oa.record;

import android.app.Activity;

import com.zhuchaooasystem.api.HttpFactory;
import com.zhuchaooasystem.api.HttpUtil;
import com.zhuchaooasystem.api.RxResultHelper;
import com.zhuchaooasystem.base.BasePresenter;
import com.zhuchaooasystem.base.SchedulersCompat;
import com.zhuchaooasystem.entity.RecordDetailEntity;

import java.util.Map;

import rx.Subscription;
import rx.functions.Action1;

/**
 * @author yinsi
 */


public class RecordDetailPresenter extends BasePresenter<RecordDetailContract.View> implements RecordDetailContract.Presenter {

    public RecordDetailPresenter(Activity activity, RecordDetailContract.View view) {
        super(activity, view);
    }


    @Override
    public void getRecordDetail(int code) {

        final Map<String, String> map = HttpUtil.getHttpMap(mActivity);
        map.put("Code", code + "");

        Subscription subscription = HttpFactory.getHttpApi().getAudionDetailInfo(map)
                .compose(SchedulersCompat.applyIoSchedulers())
                .compose(RxResultHelper.handleResult()).subscribe(new Action1<Object>() {
                    @Override
                    public void call(Object o) {
                        if (null != o) {
                            RecordDetailEntity entity = (RecordDetailEntity) o;
                            if (null != entity.getResult().getSuccessful()) {
                                mView.refreshData(entity.getResult().getSuccessful());
                            } else if (null != entity.getResult().getError()) {
                                handleError(entity.getResult().getError());
                            }
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        handleError(throwable);
                    }
                });
        addSubscrebe(subscription);
    }

}
